package com.aircms.service.impl;

import com.aircms.common.ServerResponse;
import com.aircms.dao.NewsMapper;
import com.aircms.pojo.News;
import com.aircms.pojo.NewsType;
import com.aircms.pojo.validation.BaseQueryFieldsValidation;
import com.aircms.service.INewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author air
 * @create 2018-09-30 下午4:39
 */

@Service(value = "iNewsService")
public class NewsServiceImpl implements INewsService {
    @Autowired
    private NewsMapper newsMapper;

    public ServerResponse index(BaseQueryFieldsValidation baseQueryFields, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<News> news = newsMapper.selectAll(baseQueryFields);
        PageInfo pageInfo = new PageInfo(news);
        return ServerResponse.response(pageInfo);
    }

    public ServerResponse store(News news) {
        newsMapper.insertSelective(news);
        return ServerResponse.response();
    }

    public ServerResponse update(News news) {
        if (newsMapper.updateByPrimaryKeySelective(news) > 0) {
            return ServerResponse.response();
        } else {
            return ServerResponse.response("", -1);
        }
    }

    public ServerResponse destroy(Integer id) {
        if (newsMapper.setStatus(id, -1) > 0) {
            return ServerResponse.response();
        } else {
            return ServerResponse.response("", -1);
        }
    }

    public ServerResponse show(Integer id) {
        News news = newsMapper.selectByPrimaryKey(id);
        return ServerResponse.response(news);
    }
}
